package net.siufung.boot.datasource.dto;

import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.siufung.boot.datasource.enums.DbTypes;
import net.siufung.boot.datasource.rules.IDbType;
import net.siufung.core.utils.StringPool;

import java.io.Serializable;

/**
 * @author 陈建峰
 * @since 2022/8/15 9:26 下午
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ConnDto implements Serializable {

    private static final long serialVersionUID = -7871151717090398403L;

    /**
     * 数据库类型
     */
    private IDbType dbType;

    /**
     * 模式名称
     */
    private String schemaName;

    /**
     * 连接的URL
     */
    private String url;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 获得模式名称
     * @return schemaName
     */
    public String getSchemaName() {
        if(Strings.isNullOrEmpty(schemaName)){
            if(DbTypes.POSTGRE_SQL.equals(DbTypes.get(dbType.getValue()))){
                    schemaName = StringPool.PUBLIC;
            } else if(DbTypes.ORACLE.equals(DbTypes.get(dbType.getValue()))){
                schemaName = username.toUpperCase();
            }
        }
        return schemaName;
    }
}
